package com.triones.ljr.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.triones.ljr.entity.dto.BannerDO;
import com.triones.ljr.entity.vo.BannerVO;
import com.triones.ljr.mapper.BannerMapper;
import com.triones.ljr.service.BannerService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 广告图
 */
@Service
@RequiredArgsConstructor
public class BannerServiceImpl implements BannerService {

    private final BannerMapper bannerMapper;

    @Override
    public TableDataInfo<BannerVO> pageList(String keyword, PageQuery pageQuery) {
        LambdaQueryWrapper<BannerDO> lqw = Wrappers.lambdaQuery();
        lqw.eq(BannerDO::getDeleted,0)
            .like(StringUtils.isNotBlank(keyword), BannerDO::getTitle, keyword)
            .orderByAsc(BannerDO::getOrderNum);
        Page<BannerVO> result = bannerMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public boolean add(BannerDO dto) {
        return bannerMapper.insert(dto) > 0;
    }

    @Override
    public boolean edit(BannerDO dto) {
        return bannerMapper.updateById(dto) > 0;
    }

    @Override
    public BannerVO getInfo(Long id) {
        return bannerMapper.selectVoById(id);
    }

    @Override
    public boolean remove(Long id) {
        BannerDO bannerDO = bannerMapper.selectById(id);
        if (bannerDO == null) {
            throw new ServiceException("信息不存在");
        }
        return bannerMapper.deleteById(bannerDO) > 0;
    }

    @Override
    public boolean updateShowStatus(BannerDO dto) {
        BannerDO bannerDO = bannerMapper.selectById(dto.getId());
        if (bannerDO != null) {
            bannerDO.setShowStatus(dto.getShowStatus());
            return bannerMapper.updateById(bannerDO) > 0;
        }
        return false;
    }

    @Override
    public List<BannerVO> list() {
        LambdaQueryWrapper<BannerDO> lqw = Wrappers.lambdaQuery();
        lqw.eq(BannerDO::getShowStatus,0);
        lqw.eq(BannerDO::getDeleted,0);
        lqw.orderByAsc(BannerDO::getOrderNum);
        return bannerMapper.selectVoList(lqw);
    }
}
